﻿using parkMonitor.entity;
using parkMonitor.model;
using parkMonitor.viewModel.objectTree;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace parkMonitor.server.monitorServer {
	/// <summary>号牌机监控</summary>
	public class NumMachineMonitorCtl : IMonitorCtl {
		MonitorModel monitorMd = null;

		Dictionary<string, ListMiddleParam> mapNumMachineMd = new Dictionary<string, ListMiddleParam>();    //号牌机数据

		public NumMachineMonitorCtl(MonitorModel _monitorMd) {
			monitorMd = _monitorMd;
		}

		public void init() {

		}

		public void getData() {
			IEquipments num = EquipmentSimpleFactory.ins.FindEquipment(EquipmentName.NumMachine);
			if(num == null) {
				return;
			}
			NumberMachineMessage data = num.GetMessage() as NumberMachineMessage;
			if(data == null) {
				return;
			}

			for(int i = 0; i < data.data.Count; ++i) {
				var item = data.data[i];
				if(!mapNumMachineMd.ContainsKey(item.ip)) {
					initNumMachineMd(item);
				}

				setNumMachineMd(item);
			}
		}

		public void clear() {

		}

		/// <summary>设置号牌机数据</summary>
		private void setNumMachineMd(NumberMachineNode src) {
			if(!mapNumMachineMd.ContainsKey(src.ip)) {
				return;
			}
			var md = mapNumMachineMd[src.ip];
			const int numIdx = 0;
			const int statusIdx = 1;
			md[numIdx].value = src.LicenseNum;
			md[statusIdx].value = src.status.ToString();
			md.value = src.LicenseNum;
		}

		/// <summary>初始化号牌机</summary>
		private void initNumMachineMd(NumberMachineNode src) {
			//mapNumMachineMd
			ListMiddleParam md = new ListMiddleParam() {
				type = DeviceType.NumMachine,
				imgType = DeviceType.NumMachine,
				uiType = DevTreeItemShowType.ImageValue,
				nameDesc = src.ip,
				shortNameType = src.ip
			};
			md.Add(new DevParamModel() {
				addr = MonitorModel.numMachineAddress,
				typeDesc = "号牌机",
				subTypeDesc = src.ip,
				statusDesc = "号牌",
				value = src.LicenseNum
			});
			md.Add(new DevParamModel() {
				addr = MonitorModel.numMachineAddress,
				typeDesc = "号牌机",
				subTypeDesc = src.ip,
				statusDesc = "状态",
				value = src.status.ToString(),
				showType = ParamShowType.e_cbx,
				mapCbx = new Dictionary<string, CbxModel>() {
					{EnumNumberMachineStatus.Offline.ToString(), new CbxModel(){desc = "离线", shortDesc = "离线", value = EnumNumberMachineStatus.Offline.ToString(), warnLevel=  AlarmStatus.Offline } },
					{EnumNumberMachineStatus.Normal.ToString(), new CbxModel(){desc = "在线", shortDesc = "在线", value = EnumNumberMachineStatus.Normal.ToString(), warnLevel=  AlarmStatus.Normal } },
				}
			});

			mapNumMachineMd[src.ip] = md;

			MainModel.ins.mainWin.Dispatcher.Invoke(() => {
				monitorMd.initMiddleMdToVM(MainModel.ins.mainWinVM.LstObjectTree.numMachine, md, "号牌机&" + src.ip);
			});
		}
	}
}
